local fk__juedi = fk.CreateSkill {

  name = "fk__juedi",

  tags = { Skill.Compulsory, },

}

local U = require "packages/utility/utility"


fk__juedi:addEffect(fk.TargetConfirming, {
  name = "fk__juedi",
  anim_type = "defensive",
  events = {fk.TargetConfirming},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(fk__juedi.name) and data.card.is_damage_card
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.extra_data = data.extra_data or {}
    data.extra_data.fk__juedi = data.extra_data.fk__juedi or {}
    data.extra_data.fk__juedi[tostring(player.id)] = data.extra_data.fk__juedi[tostring(player.id)] or {}
    for _, id in ipairs(player:drawCards(3, fk__juedi.name)) do
      if table.contains(player.player_cards[Player.Hand], id) then
        room:setCardMark(Fk:getCardById(id), "@@fk__juedi-inhand", 1)
        table.insertIfNeed(data.extra_data.fk__juedi[tostring(player.id)], id)
      end
    end
  end,
})

fk__juedi:addEffect(fk.CardUseFinished, {
  name = "#fk__juedi_delay",
  mute = true,
  events = {fk.CardUseFinished},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return not player.dead and data.extra_data and data.extra_data.fk__juedi and data.extra_data.fk__juedi[tostring(player.id)]
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = data.extra_data.fk__juedi[tostring(player.id)]
    local throw = table.filter(player.player_cards[Player.Hand], function (id)
      return table.contains(mark, id) and not player:prohibitDiscard(Fk:getCardById(id))
    end)
    if #throw > 0 then
      room:delay(500)
      room:throwCard(throw, fk__juedi.name, player, player)
    end
  end,
})

return fk__juedi